<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:公用控制器
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\BackendController;
use support\Request;
use think\facade\Cache;
use think\facade\Db;

class CommonController extends BackendController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function upload()
    {

        $cfg         = getConfig('website');
        $limit_types = str_replace("|", ",", $cfg['file_type']);
        $limit_size  = 1024 * $cfg['file_max_size'];
        $files       = request()->file();
        $savename    = [];
        $status      = 1;
        $msg         = '上传成功';
        $cfg        =  'filesize:' . $limit_size . '|fileExt:' . $limit_types;
        $rule        = $this->analyzeFileRule($cfg);
     
        try {

            foreach ($files as $k => $file) {

                if (!in_array($file->getUploadExtension(), $rule['ext'])) {

                    throw new \Exception("file type error");

                }
                if ($file->getSize() > $rule['size']) {
                    throw new \Exception("file size error");
                }

            }

            $file_type = "image";

            foreach ($files as $k => $file) {
                $savename[] = $this->uploadAPi($file_type, $file);
            }

           


        } catch (\Exception $e) {
            $status = 0;
            $msg    = $e->getMessage();
        }
        return json(['code' => 0, 'status' => $status, 'data' => ['src' => $savename, 'title' => $savename], 'msg' => $msg]);

    }

    private function uploadAPi($file_type, $file)
    {

        $new_file = '/files/' . $file_type . '/' . date('Y-m-d') . '/' . time() . '_' . $file->getUploadName();

        $file->move(public_path() . $new_file);
        return $new_file;

    }

    private function analyzeFileRule($rule)
    {
        $rule = explode("|", $rule);
        $size = substr($rule[0], 9);
        $ext  = substr($rule[1], 8);
        return ['size' => $size, 'ext' => explode(",", $ext)];
    }

/**
 * 自动完成下拉菜单数据源
 * @param  [type] $type [字符]
 * @return [type]       [数组]
 */
    private function jsonAuto($type, $q, $filter = null)
    {
        $q = trim($q);
        $w = null;
        if ($filter) {
            $fit         = urldecode($filter);
            $fit && $fit = json_decode($fit, true);
        }
        switch ($type) {

            //会员
            case 'member':
                $w;
                $q && $w[] = array('username', 'like', '%' . $q . '%');
                $data      = Db::name('user')->where($w)->field("u_id as id,username as  name")->limit(20)->select();
                break;

        }
        return $data;
    }

/**
 * 系统缓存数据
 * @param  [type] $type [字符]
 * @return [type]       [数组]
 */
    private function cacheData($type, $update = 0)
    {

        switch ($type) {

            //用户
            case 'member':
                $data = Db::name('user')->field('u_id as id,username as name')->cache(true)->column("name", "id");
                return $data;
                break;

        }

        return $data;
    }

    public function ajax_json_auto(Request $request)
    {
        $type   = $request->get('type');
        $q      = $request->get('q');
        $filter = $request->get('filter'); //过滤条件
        return json($this->jsonAuto($type, $q, $filter));

    }
    public function ajax_json_data(Request $request)
    {
        $type     = $request->get('type');
        $selected = $request->get('selected');
        $cache    = $this->cacheData($type);
        $selected = explode(',', $selected);
        if (!$cache) {
            return json($selected); //应对仅文本选择
        } else {
            $rs = array();
            foreach ($selected as $v) {
                $rs[$v] = $cache[$v] ? $cache[$v] : $v;
            }
            if ($rs) {
                return json($rs);
            }

        }

    }
}
